package com.virtual.rerijaapps.pet.utils;

import java.util.Calendar;
import java.util.TimeZone;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.net.Uri;
import android.provider.CalendarContract.Events;
import android.provider.CalendarContract.Reminders;

public class CalendarHelper 
{

	public static long insertEventDefaultCalendar( Activity act, String title, Calendar fecha ) 
	{
		try
		{
			ContentResolver cr = act.getContentResolver();
			ContentValues values = new ContentValues();
	        values.put( Events.DTSTART, fecha.getTimeInMillis() );
	        values.put( Events.DTEND, fecha.getTimeInMillis() );
	        values.put( Events.TITLE, title );
	        values.put( Events.DESCRIPTION, "" );
	        values.put( Events.CALENDAR_ID, 1 );
	        values.put( Events.EVENT_TIMEZONE, TimeZone.getDefault().getID() );
	        values.put( Events.ALL_DAY, 0 );
	        values.put( Events.STATUS, 1 );
	        values.put( Events.HAS_ALARM, 1 );
	        Uri uri = cr.insert( Events.CONTENT_URI, values );
	        
	        long eventID = Long.parseLong( uri.getLastPathSegment() );
	        ContentValues reminders = new ContentValues();
	        reminders.put( Reminders.EVENT_ID, eventID );
	        reminders.put( Reminders.METHOD, Reminders.METHOD_ALERT );
	        reminders.put( Reminders.MINUTES, 60 );

	        Uri uri2 = cr.insert( Reminders.CONTENT_URI, reminders );
	        
	        return eventID;
	        
		}
		catch( Exception ex )
		{
			ex.printStackTrace();
			return -1;
		}
    }
	
	public static void deleteEventDefaultCalendar( Activity act, long eventID )
	{
		if( eventID != -1 )
		{
			try
			{
				ContentResolver cr = act.getContentResolver();
				ContentValues values = new ContentValues();
				Uri deleteUri = null;
				deleteUri = ContentUris.withAppendedId( Events.CONTENT_URI, eventID );
				int rows = act.getContentResolver().delete( deleteUri, null, null );
			}
			catch( Exception ex )
			{
				ex.printStackTrace();
			}
		}
	}
	
}
